package com.zhudi.model;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于提供各种跟http有关的操作
 */
public class HttpConnection {


    public void doRequest(String message){
        System.out.println(message+"发出了请求");
    }

    public String doPost(String url, String parameter,Map<String,String> headers){


        HttpRequest httpRequest = HttpRequest.post(url)
                .body(parameter)
                .charset(CharsetUtil.UTF_8)
                .addHeaders(headers);
        HttpResponse response = httpRequest
                .execute();
        return response.body();
    }

    public String doGet(String url, Map<String,Object> parameter,Map<String,String> headers){
        //如果是get的话，还要拼接参数上去

        HttpRequest httpRequest = HttpRequest.get(url).form(parameter).charset(CharsetUtil.UTF_8).addHeaders(headers);
        String result = httpRequest.execute().body();
        return result;
    }
}
